package com.lyn.solitude.entity

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable

/**
 * @author longyn 2024/5/21
 * @version 1.0.0
 */
open class Plugin : Serializable {

    var id: Long = 0

    var uuid: String = ""

    var name: String = ""

    var version: Int = 0

    var filePath: String = ""

    var soFolderPath: String = ""

    var mainClassName: String = ""

    var time: Long = 0


    companion object {
        fun toPlugin(data: ByteArray): Plugin? {
            try {
                val inputStream = ByteArrayInputStream(data)
                val objectInputStream = ObjectInputStream(inputStream)
                val plugin = objectInputStream.readObject() as Plugin
                objectInputStream.close()
                inputStream.close()
                return plugin
            } catch (e: Throwable) {
                e.printStackTrace()
            }
            return null
        }

        fun toListPlugin(data: ByteArray): MutableList<Plugin>? {
            try {
                val inputStream = ByteArrayInputStream(data)
                val objectInputStream = ObjectInputStream(inputStream)
                val pluginList = objectInputStream.readObject() as MutableList<Plugin>
                objectInputStream.close()
                inputStream.close()
                return pluginList
            } catch (e: Throwable) {
                e.printStackTrace()
            }
            return null
        }

        fun toByteArray(any: Any): ByteArray? {
            try {
                val byteArrayOutputStream = ByteArrayOutputStream()
                val objectOutputStream = ObjectOutputStream(byteArrayOutputStream)
                objectOutputStream.writeObject(any)
                val toByteArray = byteArrayOutputStream.toByteArray()
                objectOutputStream.flush()
                objectOutputStream.close()
                byteArrayOutputStream.close()
                return toByteArray
            } catch (e: Throwable) {
                e.printStackTrace()
            }
            return null
        }
    }


}